package org.jeecg.modules.wlhy.home.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.enums.ExamineStatusEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.third.ecloud.sdk.util.DateUtil;
import org.jeecg.modules.wlhy.home.service.HyHomeService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.shipping.constant.ShippingStatus;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class HyHomeServiceImpl implements HyHomeService {

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired
    private IHyDriverService hyDriverService;

    @Autowired
    private IHyVehicleService hyVehicleService;

    @Lazy
    @Resource
    private RedisUtil redisUtil;

    @Override
    public Result getQuickStartData(Integer mode) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -6);

        JSONObject jsonObject = new JSONObject();
        //获取近半年运输数据
        List<HyShippingNote> hyShippingNoteList = this.hyShippingNoteService.list(new LambdaQueryWrapper<HyShippingNote>()
                .gt(HyShippingNote::getCreateTime, calendar.getTime()));

        //获取当天的运输数据
        List<HyShippingNote> hyShippingNoteList2 = this.hyShippingNoteService.list(new LambdaQueryWrapper<HyShippingNote>()
              .ge(HyShippingNote::getCreateTime, DateUtil.getDay()+" 00:00:00").le(HyShippingNote::getCreateTime, new Date().toString()));

        Calendar calendar1 = Calendar.getInstance();  // 创建Calendar实例
        calendar1.add(Calendar.DAY_OF_YEAR, -1);  // 减去1天
        Date yesterday = calendar1.getTime();  // 获取昨天的日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  // 定义日期格式
        String yesterdayStr = formatter.format(yesterday);  // 格式化昨天的日期



        //获取昨天的运单数据数量
        List<HyShippingNote> hyShippingNoteList3 = this.hyShippingNoteService.list(new LambdaQueryWrapper<HyShippingNote>().ge(HyShippingNote::getCreateTime, (yesterdayStr+" 00:00:00")).le(HyShippingNote::getCreateTime, (yesterdayStr+" 23:59:59")));

        List<HyDriver> hyDriverList = this.hyDriverService.list(new LambdaQueryWrapper<HyDriver>()
                .gt(HyDriver::getCreateTime, calendar.getTime()));

        List<HyVehicle> hyVehicleList = this.hyVehicleService.list(new LambdaQueryWrapper<HyVehicle>()
                .gt(HyVehicle::getCreateTime, calendar.getTime()));

        //组装数据
        //运输管理：
        long loadCount = 0L;
        long unloadCount = 0L;
        long signCount = 0L;
        long needPayCount = 0L;
        long unUploadCount = 0L;
        long uploadFailCount = 0L;
        BigDecimal goodsWeightLoadingCount = new BigDecimal(0);//今日装车重量
        long shippingNoteCount = 0L;//今日运单数
        long yesterdayShippingNoteCount = 0L;//昨日运单数
        BigDecimal yesterdayGoodsWeightLoadingCount = new BigDecimal(0);//昨日装车重量
        for (HyShippingNote s : hyShippingNoteList) {
            if (s.getShippingStatus().equals(ShippingStatus.运输中.getValue()) || s.getShippingStatus().equals(ShippingStatus.装货中.getValue())) {
                loadCount++;
            } else if (s.getShippingStatus().equals(ShippingStatus.已交货.getValue())) {
                unloadCount++;
            } else if (s.getShippingStatus().equals(ShippingStatus.已签收.getValue()) && "1".equals(s.getShippingPayStatus())) {
                signCount++;
            } else if ("4".equals(s.getShippingPayStatus())) {
                needPayCount++;
            }
            if (s.getStatusDtLoading()!=null&&!s.getStatusDtLoading().equals("")) {
                //今日运单装载量
                if (DateUtil.getDay().contains(formatter.format(s.getStatusDtLoading()))) {
                    goodsWeightLoadingCount=goodsWeightLoadingCount.add(s.getGoodsWeightLoading());
                }
                //昨日运单装载量
                if (formatter.format(s.getStatusDtLoading()).contains(yesterdayStr)) {
                    yesterdayGoodsWeightLoadingCount=yesterdayGoodsWeightLoadingCount.add(s.getGoodsWeightLoading());
                }
            }
            //监管上报统计
            if (StrUtil.isEmpty(s.getUploadStatus()) || "0".equals(s.getUploadStatus())) {
                unUploadCount++;
            } else if (StrUtil.isNotEmpty(s.getUploadStatus()) && !"1".equals(s.getUploadStatus())) {
                uploadFailCount++;
            }
        }
        for (HyShippingNote s : hyShippingNoteList2) {
           if (s.getShippingStatus().equals(ShippingStatus.运输中.getValue())||s.getShippingStatus().equals(ShippingStatus.已交货.getValue())||s.getShippingStatus().equals(ShippingStatus.已签收.getValue())) {
               shippingNoteCount++;
           }

        }
        for (HyShippingNote s : hyShippingNoteList3) {
            if (s.getShippingStatus().equals(ShippingStatus.运输中.getValue())||s.getShippingStatus().equals(ShippingStatus.已交货.getValue())||s.getShippingStatus().equals(ShippingStatus.已签收.getValue())) {
                yesterdayShippingNoteCount++;
            }

        }
        //今日运单数据
        jsonObject.put("goodsWeightLoadingCount", goodsWeightLoadingCount);
        jsonObject.put("shippingNoteCount", shippingNoteCount);
        //昨日运单数据
        jsonObject.put("yesterdayGoodsWeightLoadingCount", yesterdayGoodsWeightLoadingCount);
        jsonObject.put("yesterdayShippingNoteCount", yesterdayShippingNoteCount);
        //1.多少单已装货
        //2.多少单已交货未签收
        jsonObject.put("loadCount", loadCount);
        jsonObject.put("unloadCount", unloadCount);

        //运单对账&支付
        //1.多少单已签收未对账
        //2.多少单需要支付
        jsonObject.put("signCount", signCount);
        jsonObject.put("needPayCount", needPayCount);

        //监管上报数据统计
        jsonObject.put("unUploadCount", unUploadCount);
        jsonObject.put("uploadFailCount", uploadFailCount);

        //司机管理
        //1.多少司机未审核
        //2.多少司机审核未通过
        //ExamineStatusEnum
        //EXAMINE_STATUS_TOSUBMITTED("0", "待提交"),
        //EXAMINE_STATUS_TOREVIEWED("1", "待审核"),
        //EXAMINE_STATUS_APPROVED("2", "审核通过"),
        //EXAMINE_STATUS_AUDIT_FAILURE("3", "审核失败");
        long driverNotApprovedCount = 0L;
        long driverApprovedFailCount = 0L;
        for (HyDriver d : hyDriverList) {
            if (d.getExamineStatus().equals(ExamineStatusEnum.EXAMINE_STATUS_TOREVIEWED.getCode())) {
                driverNotApprovedCount++;
            } else if (d.getExamineStatus().equals(ExamineStatusEnum.EXAMINE_STATUS_AUDIT_FAILURE.getCode())) {
                driverApprovedFailCount++;
            }
        }
        jsonObject.put("driverNotApprovedCount", driverNotApprovedCount);
        jsonObject.put("driverApprovedFailCount", driverApprovedFailCount);

        //车辆管理
        //1.多少车辆未审核
        long vehicleNotApprovedCount = 0L;
        long vehicleApprovedFailCount = 0L;
        for (HyVehicle v : hyVehicleList) {
            if (v.getExamineStatus().equals(ExamineStatusEnum.EXAMINE_STATUS_TOREVIEWED.getCode())) {
                vehicleNotApprovedCount++;
            } else if (v.getExamineStatus().equals(ExamineStatusEnum.EXAMINE_STATUS_AUDIT_FAILURE.getCode())) {
                vehicleApprovedFailCount++;
            }
        }
        jsonObject.put("vehicleNotApprovedCount", vehicleNotApprovedCount);
        jsonObject.put("vehicleApprovedFailCount", vehicleApprovedFailCount);

        //数据上报
        //无

        //申请开票
        //无


        //再取出登录时的一些信息
        LoginUser loginUser = UserUtil.getLoginUser();
        String loginAddress = (String)this.redisUtil.get("loginUserAddr:" + loginUser.getUsername());
        String loginIP = (String)this.redisUtil.get("loginUserIP:" + loginUser.getUsername());

        jsonObject.put("loginAddress", loginAddress);
        jsonObject.put("loginIP", loginIP);

        return Result.ok(jsonObject);
    }
}
